<?php

namespace App\Jobs;

use App\Models\FileGoods;
use App\Models\FileStatus;
use App\Utils\Code;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ImportGoods implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * @var FileGoods
     */
    private $file;

    /**
     * Create a new job instance.
     *
     * @param FileGoods $file
     */
    public function __construct(FileGoods $file)
    {
        $this->file = $file;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {
            \Excel::import(new \App\Imports\GoodsImport($this->file), public_path("/storage/excels/{$this->file->path}"));
            $this->file->status = 1;
            $this->file->save();
        } catch (\Exception $exception) {
            Code::exceptionLog($exception);
            $this->file->status = 2;
            $this->file->save();
        }
    }
}
